package com.power.sifa.approve.executor.impl;

import com.plf.common.domain.ResponseDTO;
import com.plf.common.domain.YesNo;
import com.power.flowable.api.IPersonalFeign;
import com.power.sifa.approve.dao.Apl_admre_auditDao;
import com.power.sifa.approve.model.pojo.vo.ProcessVariablesVo;
import com.power.sifa.util.executor.AbstractPostExecutor;
import com.power.sifa.approve.model.Apl_admre_audit;
import com.power.sifa.approve.model.Apl_admreview;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.model.enumeration.Tp_proctype;
import com.power.sifa.approve.model.enumeration.Tp_proctypeEnum;
import com.power.sifa.approve.service.Apl_admreviewService;
import com.power.sifa.approve.service.Apl_arrive_detailService;
import com.power.sifa.approve.service.Apl_mainService;
import com.power.sifa.approve.service.BusinessApproveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 中止完成服务
 */
@Component
@Slf4j
public class HearAuditRest extends AbstractPostExecutor {

    // 用户id
    private String userid;
    // 环节分类标识
    private String category;
    // 用户名
    private String username;
    // 单位id
    private String sectionid;
    // 单位名称
    private String sectionname;
    // 部门id
    private String deptid;
    // 部门名称
    private String deptname;
    // 处理意见
    private String remark;
    // 行政区划
    private String adid;
    // 环节名称
    private String taskname;

    @Resource
    private Apl_admreviewService apl_admreviewService;
    @Resource
    private BusinessApproveService businessApproveService;
    @Resource
    private Apl_admre_auditDao apl_admre_auditDao;
    @Resource
    private Apl_mainService apl_mainService;
    @Resource
    private IPersonalFeign iPersonalFeign;

    @Override
    public void init(Map<String, Object> initdata) {
        this.userid = (String) initdata.get("userid");
        this.category = (String) initdata.get("category");
        this.username = (String) initdata.get("username");
        this.sectionid = (String) initdata.get("sectionid");
        this.sectionname = (String) initdata.get("sectionname");
        this.deptid = (String) initdata.get("deptid");
        this.deptname = (String) initdata.get("deptname");
        this.remark = (String) initdata.get("remark");
        this.adid = (String) initdata.get("adid");
        this.taskname = (String) initdata.get("taskname");
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> execute() throws Exception {
        // 1. 生成和保存三级审批文书
        Apl_admre_audit auditObj = this.businessApproveService.createThreeLevelApprove(super.applyid, this.userid, this.username, Tp_proctype.HEAR_REST);
        Apl_main faMainObj = this.apl_mainService.getById(auditObj.getApplyid());
        // 创建送达通知
//        this.apl_arrive_detailService.createSaveNotice(faMainObj.getId(), faMainObj.getAcceptcode(), faMainObj.getAcceptuserid(),
//                7, faMainObj.getAcceptcode() + "已审批通过，请及时送达《中止行政复议通知书》", Tp_procmaterial.HEAR_HANGUP);

        // 2. 删除一个子状态
        Apl_admreview admreObj = this.apl_admreviewService.getById(auditObj.getApplyid());
        String sonStatus = this.businessApproveService.delSonStatus(admreObj.getSonstatus(), Tp_proctypeEnum.HEAR_REST.getStatus());

        admreObj.setSonstatus(sonStatus);
        // 更新主表状态为已中止
        admreObj.setIsrest(YesNo.YES);
        Date now = new Date();
        admreObj.setRestdate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
        this.apl_admreviewService.saveOrUpdate(admreObj);

        // 3. 更新子流程主表状态为已中止
        this.apl_admre_auditDao.updateIsrest(auditObj.getApplyid(), YesNo.YES);

        // 4. 父流程变量存储一个中止时间
        Map<String, Object> variableMap = new HashMap<>(16);
        variableMap.put(ProcessVariablesVo.RESTDATE, new SimpleDateFormat("yyyy-MM-dd").format(now));
        ResponseDTO res = this.iPersonalFeign.saveVariable(faMainObj.getInstid(), variableMap);
        if (0 != res.getCode()) {
            throw new RuntimeException(res.getMsg());
        }


        // 刷新文书日期
        this.businessApproveService.processEndedUpdatePreviewDoc(auditObj.getApplyid(), Tp_proctype.HEAR_REST, null);
        // 文书盖章
        this.businessApproveService.templateStamp(auditObj.getApplyid(), Tp_proctype.HEAR_REST);
        return null;
    }
}
